package com.agy.filemanager.service.impl;

import com.agy.filemanager.common.StringUtils;
import com.agy.filemanager.entity.ProMainType;
import com.agy.filemanager.entity.ProMainTypeExample;
import com.agy.filemanager.entity.ProType;
import com.agy.filemanager.entity.ProTypeExample;
import com.agy.filemanager.entity.TreeModel;
import com.agy.filemanager.mapper.ProMainTypeMapper;
import com.agy.filemanager.mapper.ProTypeMapper;
import com.agy.filemanager.service.ProMainTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目大类Service实现类
 */
@Service
public class ProMainTypeServiceImpl implements ProMainTypeService{

    @Autowired
    private ProMainTypeMapper proMainTypeMapper;


    /**
     * 保存项目大类
     * @param name
     * @return
     */
    @Override
    public String insertProMainType(String name) {
        String id = StringUtils.getUUID();
        ProMainType entity = new ProMainType();
        entity.setId(id);
        entity.setName(name);
        try {
            int sort = this.proMainTypeMapper.countByExample(new ProMainTypeExample()) + 1;
            entity.setSort(sort);
            this.proMainTypeMapper.insertSelective(entity);
            return id;
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public void updateProMainTypeById(ProMainType proMainType) {
        proMainTypeMapper.updateByPrimaryKeySelective(proMainType);
    }

    /**
     * 根据id删除项目大类
     * @param id
     */
    @Override
    public void deleteProMainTypeById(String id) {
        proMainTypeMapper.deleteByPrimaryKey(id);
    }

    /**
     * 查询项目大类树
     * @return
     */
    @Override
    public List<TreeModel> getProMainTypeTreeNode() {
        //查询所有
        ProMainTypeExample example = new ProMainTypeExample();
        example.setOrderByClause("sort desc");
        List<ProMainType> proMainTypes = proMainTypeMapper.selectByExample(example);
        List<TreeModel> treeNodes = new ArrayList<>();
        for (ProMainType mainType: proMainTypes) {
            TreeModel node = new TreeModel();
            node.setId(mainType.getId());
            node.setText(mainType.getName());
            treeNodes.add(node);
        }
        return treeNodes;
    }
}
